package com.pzj.httpclient;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.cache.CachingHttpClient;

public class HttpConnection implements Runnable {
	private Log log = LogFactory.getLog(getClass());
	
	private String url;
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void run() {
		
		HttpClient client = new CachingHttpClient();
		HttpUriRequest request = new HttpPost(url);
		
		try {
			HttpResponse response = client.execute(request);
			HttpEntity entity = response.getEntity();
			InputStream in = entity.getContent();
			BufferedInputStream bin = new BufferedInputStream(in);
			BufferedReader reader = new BufferedReader(new InputStreamReader(bin));
			String line = null;
			while((line = reader.readLine()) != null) {
				System.out.println(new String(line.getBytes(), "gb2312"));
			}
			
		} catch (ClientProtocolException e) {
			log.error(e.getMessage(), e);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
		
	}

}
